# Create your models here.
# 控制器
from django.urls import path
from django.http import HttpResponse, JsonResponse
import datetime
import time
import json
from django.conf.urls import url
from . import views
from airconfig.config import config
from customer.customer import Room
from scheduler.schedule import sche




# 主控
class Master():
    def SetPara(self,default_work_mode, temp_h, temp_l, default_goal_temp, default_fan_speed, feeRate_l, feeRate_m, feeRate_h):
        if default_work_mode == -1 :
            config.cold_sub = temp_l
            config.cold_sup = temp_h
        else:
            config.hot_sub = temp_l
            config.hot_sup = temp_h
        config.default_work_mode = default_work_mode
        if default_work_mode == -1:
            config.default_goal_temp_cold = default_goal_temp
        else:
            config.default_goal_temp_hot = default_goal_temp
        config.default_fan_speed = default_fan_speed
        config.temp_rate_speed[0] = feeRate_l
        config.temp_rate_speed[1] = feeRate_m
        config.temp_rate_speed[2] = feeRate_h


# 监控
class Monitor():
    def CheckRoomState(self, list_room):
        res = dict()
        res["data"] = dict()
        res["error_code"] = 0
        res["data"] = []

        for i in list_room:
            state = sche.get_request_pos(i)
            if state == 'service':
                state = 'server'
            elif state == 'pause':
                state = 'tempUp'
            temp = dict()
            request = sche.get_request(i)
            temp["roomId"] = i
            temp["state"] = state
            temp["mode"] = request.wordmode
            temp["targetTemp"] = request.targetTemp
            temp["currentTemp"] = request.currentTemp
            temp["fanSpeed"] = request.fanSpeed
            res["data"].append(temp)
        return res


# 管理员
class Administor():
    def __init__(self):
        self.user = "rjgc"
        self.passwd = "rjgc"

    def login(self, user, passwd):
        if user == self.user and passwd == self.passwd:
            return 0
        else:
            return 1

    def PowerOn(self):
        views.powerFlag = True

    def StartUp(self):

        for loop in config.room_list:
            room = Room()
            room.roomid = loop

            if config.default_work_mode == -1:
                room.temp_highLimit = config.cold_sup
                room.temp_lowLimit = config.cold_sub
                if loop < '105':
                    room.currenttemp = config.default_current_temp_cold[loop]
            else:
                room.temp_highLimit = config.hot_sup
                room.temp_lowLimit = config.hot_sub
                if loop < '105':
                    room.currenttemp = config.default_current_temp_hot[loop]

            views.room_list.append(room)
        views.masterFlag = True



admin = Administor()
master = Master()


def trans(roomid):
    return (int(roomid[0])-1)*10 + int(roomid[2]) - 1

def log_in(request):
    request.params = json.loads(request.body)
    user = request.params["user"]
    passwd = request.params["passwd"]
    res = {}
    res["error_code"] = admin.login(user, passwd)
    print(res["error_code"])
    return JsonResponse(res)


def poweron(request):
    admin.PowerOn()
    return JsonResponse({'error_code': 0})


def setpara(request):
    if views.powerFlag:
        request.params = json.loads(request.body)
        temp_h = int(request.params["temp_h"])
        temp_l = int(request.params["temp_l"])
        default_work_mode = request.params["mode"]
        default_goal_temp = int(request.params["defalut_targetTemp"])
        default_fan_speed = request.params["default_fanSpeed"]
        feeRate_l = request.params["feeRate_l"]
        feeRate_m = request.params["feeRate_m"]
        feeRate_h = request.params["feeRate_h"]
        master.SetPara(default_work_mode, temp_h, temp_l, default_goal_temp, default_fan_speed, feeRate_l, feeRate_m, feeRate_h)
        return JsonResponse({'error_code': 0})
    else:
        return JsonResponse({'error_code': 1, 'data': '请开启电源'})


def startup(request):
    if views.powerFlag:
        admin.StartUp()
        return JsonResponse({'error_code': 0})
    else:
        return JsonResponse({'error_code': 1, 'data': '请开启电源'})


def checkroomstate(request):
    if views.masterFlag:
        request.params = json.loads(request.body)
        list_Room = request.params["list_Room"]
        monitor = Monitor()
        res = monitor.CheckRoomState(list_Room)
        return JsonResponse(res)
    else:
        return JsonResponse({'error_code': 1, 'data': '请开启主控机'})



urlpatterns = [
            path('signup/', log_in),
            path('setpara/', setpara),
            path('poweron/', poweron),
            path('startup/', startup),
            path('checkroomstate/', checkroomstate)
        ]